package com.kongge.viewdisplayframeworklib.ui.view.viewscroller;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.kongge.viewdisplayframeworklib.R;

/**
 * author:kongge
 * date:2018/9/13
 * layout:
 * description:
 */

public class PageIndex extends View {

    public static final int CENTER = 1;
    public static final int LEFT = 2;
    public static final int RIGHT = 2;

    public static final int TYPE_RECT = 1;
    public static final int TYPE_CIRCLE = 2;

    private int count;
    private int currentIndex;
    private float width = 16f;
    private float height = 8f;
    private float space = 8f;
    private Paint mCurrentPaint = new Paint();
    private Paint mDefaultPaint = new Paint();
    private int position = CENTER;
    private int type = TYPE_RECT;

    public PageIndex(Context context) {
        super(context);
    }

    public PageIndex(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    private void init() {
        height = getResources().getDimensionPixelOffset(R.dimen.pageindex_height);
        width = getResources().getDimensionPixelOffset(R.dimen.pageindex_width);
        space = getResources().getDimensionPixelOffset(R.dimen.pageindex_space);
        mCurrentPaint.setColor(getResources().getColor(R.color.pageindex_current_color));
        mDefaultPaint.setColor(getResources().getColor(R.color.pageindex_default_color));
        mCurrentPaint.setAntiAlias(true);
        mDefaultPaint.setAntiAlias(true);
    }

    public void setPosition(int position) {
        this.position = position;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float x = 0;
        if (position == CENTER) {
            x = (canvas.getWidth() - (width * count) - space * (count - 1)) / 2;
        } else if (position == LEFT) {
            x = space;
        } else {
            x = canvas.getWidth() - (width * count) - space * (count - 1);
        }

        float y = 0;
        for (int i = 0; i < count; i++) {
            if (count == 1) {

            } else if (i == currentIndex) {
                drawGraph(type, x, y, width, height, canvas, mCurrentPaint);
            } else {
                drawGraph(type, x, y, width, height, canvas, mDefaultPaint);
            }
            x += (width + space);
        }
    }

    private void drawGraph(int type, float x, float y, float width, float height, Canvas canvas, Paint paint) {
        switch (type) {
            case TYPE_RECT:
                canvas.drawRect(x, y, x + width, y + height, paint);
                break;
            case TYPE_CIRCLE:
                canvas.drawCircle(x + (width / 2), y, width / 2, paint);
                break;
        }
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getCurrentIndex() {
        return currentIndex;
    }

    public void setCurrentIndex(int currentIndex) {
        this.currentIndex = currentIndex;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setCurrentColor(int color) {
        mCurrentPaint.setColor(color);
    }

    public void setDefaultColor(int color) {
        mDefaultPaint.setColor(color);
    }
}
